package com.yyq.adms.usermanage.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yyq.adms.sysmanage.pojo.SysParameters;
import com.yyq.adms.usermanage.bpo.SysDeptsBpo;
import com.yyq.adms.usermanage.bpo.SysUsersBpo;
import com.yyq.adms.usermanage.pojo.SysDepts;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.util.DepartmentUtil;
import com.yyq.sos.util.SysParametersUtil;


@Controller("sysDeptsAction")
@Scope("prototype")
public class SysDeptsAction extends BaseAction {
	
	SysDepts po = new SysDepts();
	private SysDeptsBpo bpo ;
	private SysUsersBpo ubpo ;
	
	@Resource(name="sysDeptsBpo")
	public void setBpo(SysDeptsBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}

	@Resource(name="sysUsersBpo")
	public void setUbpo(SysUsersBpo ubpo) {
		this.ubpo = ubpo;
	}

	/**
	 * Action操作入口
	 */
	public String execute() throws Exception {
		super.setBpo(bpo);
		super.setObj(po);
		//this.setBpo(ubpo);
		String s = super.execute();
		this.setPo((SysDepts)this.getObj());
		return s;
	}
	
	public String update() {
		super.update();
		DepartmentUtil.getInstance().delete(po.getDeptCname());
		this.setPo((SysDepts) super.getObj());
		DepartmentUtil.getInstance().add(po.getDeptCname(), po.getId());
		return ActionType.ACTION_MSG;
	}
	
	public String add() {
		String level = (String)bpo.max("deptLevel", "parentId="+po.getParentId());
		if(level != null && level.length()==2)
			level = String.valueOf(Long.parseLong(level)+1);
		else if(level!=null && level.length()==4)
			level = String.valueOf(Long.parseLong(level)+1);
		else if(level==null && (po.getParentId()==null || po.getParentId().longValue()==0))
			level = "10";
		else if(level==null && po.getParentId().longValue()!=0)
		{
			SysDepts obj = (SysDepts)bpo.findByPk(Long.parseLong(po.getParentId()+""));
			level = obj.getDeptLevel()+"10";
		}
		po.setDeptLevel(level);
		super.add();
		DepartmentUtil.getInstance().add(po.getDeptCname(), po.getId());
		this.setPo((SysDepts)super.getObj());
		this.setMsg("{processId:\"1\",msg:\"添加成功\",id:\""+po.getId()+"\"}");
		return ActionType.ACTION_MSG;
	}

	public String delete() {
		Object ids = this.getFormValue("ids");
		if(ids!=null && ((String[])ids).length>0){
			if(ubpo.delete((java.io.Serializable[])ids)){
				DepartmentUtil.getInstance().delete(po.getDeptCname());
				this.setMsg("{processId:\"1\",msg:\"删除成功\"}");
			}else{
				this.setMsg("{processId:\"-1\",msg:\"删除失败\"}");
			}
		}else{
			this.setMsg("{processId:\"-1\",msg:\"删除失败\"}");
		}
		return ActionType.ACTION_MSG;
	}
	
	/*public String mainList() {
		bpo.list(this.getRealPath());
		return super.mainList();
	}*/

	public SysDepts getPo() {
		return po;
	}

	public void setPo(SysDepts po) {
		this.po = po;
	}
	
	@Override
	public String goTree() {
		String tree = bpo.getTree();
		PrintWriter out = null;
		try {
			HttpServletResponse rs = this.getResponse();
			rs.setCharacterEncoding("UTF-8");
			rs.setContentType("application/json");
			out = rs.getWriter();
			out.write(tree);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(out != null){
				out.close();
			}
		}
		return null;
	}
}
